package com.cluster.platform.service.impl;

import com.cluster.platform.common.api.CommonPage;
import com.cluster.platform.common.api.CommonResult;
import com.cluster.platform.common.api.DictionaryEnums;
import com.cluster.platform.common.api.ResultCode;
import com.cluster.platform.mapper.PmsSecondProductMapper;
import com.cluster.platform.model.PmsSecondProduct;
import com.cluster.platform.service.PmsSecondProductService;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.UUID;

/**
 * @ClassName: PmsSecondProductServiceImpl
 * @Author: xiezhongping
 * @Date: 2021/2/19 16:44
 * @修改日志
 */
@Service
public class PmsSecondProductServiceImpl implements PmsSecondProductService {

    @Autowired
    PmsSecondProductMapper pmsSecondProductMapper;

    @Override
    public CommonResult<Object> addPmsSecondProduct(PmsSecondProduct pmsSecondProduct) {
        if (!Optional.ofNullable(pmsSecondProduct).isPresent()) {
            return CommonResult.failed(ResultCode.PARAM_NULL);
        }
        pmsSecondProduct.setId(UUID.randomUUID().toString().replace("-", ""));
        pmsSecondProduct.setInsertDate(new Date());
        pmsSecondProduct.setStatus(DictionaryEnums.USABLE.getCode());
        int insertStatus = pmsSecondProductMapper.insertSelective(pmsSecondProduct);
        return insertStatus > 0 ? CommonResult.success(ResultCode.ADD_SUCCESS) : CommonResult.failed(ResultCode.ADD_FAIL);
    }

    @Override
    public CommonResult<Object> delPmsSecondProduct(String id, String username) {
        if (StringUtils.isBlank(id) || StringUtils.isBlank(username)) {
            return CommonResult.failed(ResultCode.PARAM_NULL);
        }
        PmsSecondProduct pmsSecondProduct = new PmsSecondProduct();
        pmsSecondProduct.setStatus(DictionaryEnums.INVALID.getCode());
        pmsSecondProduct.setId(id);
        pmsSecondProduct.setUpdateUser(username);
        int updateStatus = pmsSecondProductMapper.updateByPrimaryKeySelective(pmsSecondProduct);
        return updateStatus > 0 ? CommonResult.success(ResultCode.SUCCESS) : CommonResult.failed(ResultCode.UPDATE_FAIL);
    }

    @Override
    public CommonResult<Object> delBatchPmsSecondProduct(List<String> ids, String username) {
        if (ids.isEmpty() || StringUtils.isBlank(username)) {
            return CommonResult.failed(ResultCode.PARAM_NULL);
        }
        int updateStatus = pmsSecondProductMapper.delBatch(ids, username);
        return updateStatus > 0 ? CommonResult.success(ResultCode.SUCCESS) : CommonResult.failed(ResultCode.UPDATE_FAIL);
    }

    @Override
    public CommonResult<Object> deletePmsSecondProduct(String id) {
        if (StringUtils.isBlank(id)) {
            return CommonResult.failed(ResultCode.PARAM_NULL);
        }
        int deleteStatus = pmsSecondProductMapper.deleteByPrimaryKey(id);
        return deleteStatus > 0 ? CommonResult.success(ResultCode.SUCCESS) : CommonResult.failed(ResultCode.UPDATE_FAIL);
    }

    @Override
    public CommonResult<Object> deleteBatchPmsSecondProduct(List<String> ids) {
        if (ids.isEmpty()) {
            return CommonResult.failed(ResultCode.PARAM_NULL);
        }
        int deleteStatus = pmsSecondProductMapper.deleteBatch(ids);
        return deleteStatus > 0 ? CommonResult.success(ResultCode.SUCCESS) : CommonResult.failed(ResultCode.UPDATE_FAIL);
    }

    @Override
    public CommonResult<Object> updatePmsSecondProduct(PmsSecondProduct pmsSecondProduct) {
        if (!Optional.ofNullable(pmsSecondProduct).isPresent()) {
            return CommonResult.failed(ResultCode.PARAM_NULL);
        }
        int updateStatus = pmsSecondProductMapper.updateByPrimaryKeySelective(pmsSecondProduct);
        return updateStatus > 0 ? CommonResult.success(ResultCode.SUCCESS) : CommonResult.failed(ResultCode.UPDATE_FAIL);

    }

    @Override
    public CommonResult<Object> searchPmsSecondProduct(String id) {
        if (StringUtils.isBlank(id)) {
            return CommonResult.failed(ResultCode.PARAM_NULL);
        }
        PmsSecondProduct pmsSecondProduct = pmsSecondProductMapper.selectByPrimaryKey(id);
        if (!Optional.ofNullable(pmsSecondProduct).isPresent()) {
            return CommonResult.success(ResultCode.NO_RECORD);
        }
        return CommonResult.success(pmsSecondProduct);
    }

    @Override
    public CommonResult<Object> searchPmsSecondProductByLimit(Integer pageSize, Integer pageNum) {
        PageHelper.startPage(pageNum, pageSize);
        List<PmsSecondProduct> pmsSecondProducts = pmsSecondProductMapper.searchAll();
        if (pmsSecondProducts.isEmpty()) {
            return CommonResult.success(ResultCode.NO_RECORD);
        }
        return CommonResult.success(CommonPage.restPage(pmsSecondProducts));
    }

    @Override
    public CommonResult<Object> searchPmsSecondProductByStatus(Integer pageSize, Integer pageNum) {
        PageHelper.startPage(pageNum, pageSize);
        List<PmsSecondProduct> pmsSecondProducts = pmsSecondProductMapper.searchAllByStatus();
        if (pmsSecondProducts.isEmpty()) {
            return CommonResult.success(ResultCode.NO_RECORD);
        }
        return CommonResult.success(CommonPage.restPage(pmsSecondProducts));
    }

    @Override
    public CommonResult<Object> searchPmsSecondProductByName(String name, Integer status, Integer pageSize, Integer pageNum) {
        if (StringUtils.isBlank(name) || !Optional.ofNullable(status).isPresent() || !Optional.ofNullable(pageSize).isPresent() || !Optional.ofNullable(pageNum).isPresent()) {
            return CommonResult.failed(ResultCode.PARAM_NULL);
        }
        PageHelper.startPage(pageNum, pageSize);
        List<PmsSecondProduct> pmsSecondProducts = pmsSecondProductMapper.searchPmsSecondProductByName(name, status);
        if (pmsSecondProducts.isEmpty()) {
            return CommonResult.success(ResultCode.NO_RECORD);
        }
        return CommonResult.success(CommonPage.restPage(pmsSecondProducts));
    }
}
